package cn.yhjz.platform.system.service.impl;

import cn.yhjz.common.Pager;
import cn.yhjz.common.util.IdUtil;
import cn.yhjz.platform.system.entity.SysUser;
import cn.yhjz.platform.system.entity.SysUserAvatar;
import cn.yhjz.platform.system.mapper.SysUserMapper;
import cn.yhjz.platform.system.vo.SysUserAvatarVo;
import cn.yhjz.platform.system.mapper.SysUserAvatarMapper;
import cn.yhjz.platform.system.service.ISysUserAvatarService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = {Exception.class})
public class SysUserAvatarServiceImpl implements ISysUserAvatarService {

    @Autowired
    private SysUserAvatarMapper sysUserAvatarMapper;
    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public Pager getPage(Long currentPage, Long pageSize, SysUserAvatar sysUserAvatar) {
        Pager pager = new Pager();
        Long count = sysUserAvatarMapper.getPageCount(sysUserAvatar);
        Long pageOffset = (currentPage - 1) * pageSize;
        List<SysUserAvatarVo> records = sysUserAvatarMapper.getPage(pageOffset, pageSize, sysUserAvatar);
        pager.setRows(records);
        pager.setCurrentPage(currentPage);
        pager.setPageSize(pageSize);
        pager.setTotalPageCount(count);
        return pager;
    }

    @Override
    public SysUserAvatar insert(SysUserAvatar sysUserAvatar) {
        int res = sysUserAvatarMapper.insert(sysUserAvatar);
        if (res > 0) {
            return sysUserAvatar;
        } else {
            return null;
        }
    }

    @Override
    public List<SysUserAvatar> select(SysUserAvatar sysUserAvatar) {
        return sysUserAvatarMapper.select(sysUserAvatar);
    }

    @Override
    public List<SysUserAvatarVo> selectVo(SysUserAvatar sysUserAvatar) {
        return sysUserAvatarMapper.selectVo(sysUserAvatar);
    }

    @Override
    public SysUserAvatar selectOne(SysUserAvatar sysUserAvatar) {
        return sysUserAvatarMapper.selectOne(sysUserAvatar);
    }

    @Override
    public SysUserAvatarVo selectOneVo(SysUserAvatar sysUserAvatar) {
        return sysUserAvatarMapper.selectOneVo(sysUserAvatar);
    }

    @Override
    public SysUserAvatarVo selectById(Long id) {
        return sysUserAvatarMapper.selectById(id);
    }

    @Override
    public int deleteById(Long id) {
        return sysUserAvatarMapper.deleteById(id);
    }

    @Override
    public int updateByIdWithNull(SysUserAvatar sysUserAvatar) {
        return sysUserAvatarMapper.updateByIdWithNull(sysUserAvatar);
    }

    @Override
    public int updateById(SysUserAvatar sysUserAvatar) {
        return sysUserAvatarMapper.updateById(sysUserAvatar);
    }

    @Override
    public int update(SysUserAvatar entity, SysUserAvatar param) {
        return sysUserAvatarMapper.update(entity, param);
    }

    @Override
    public int updateWithNull(SysUserAvatar entity, SysUserAvatar param) {
        return sysUserAvatarMapper.updateWithNull(entity, param);
    }

    /**
     * 更新一个用户的头像
     *
     * @param entity
     * @return
     */
    @Override
    public String updateUserAvatar(SysUserAvatar entity) {
        //先删除之前的
        sysUserAvatarMapper.deleteByUserCode(entity.getUserCode());
        //重新新增一个
        entity.setId(null);
        entity.setCreatedTime(new Date());
        String newFileCode = IdUtil.generate();
        entity.setFileCode(newFileCode);
        //更新到用户表
        SysUser sysUserParam = new SysUser();
        sysUserParam.setUserCode(entity.getUserCode());
        SysUser sysUserEntity = new SysUser();
        sysUserEntity.setAvatarFileCode(newFileCode);
        sysUserEntity.setAvatarFileHash(entity.getHashcode());
        sysUserMapper.update(sysUserEntity, sysUserParam);
        sysUserAvatarMapper.insert(entity);
        return newFileCode;
    }
}
